#!/usr/bin/python3.4
# -*- coding: utf-8 -*-
"""
    作者:jinayang
    网站:yangshengliang.com
    版本:1.0
    时间: 2016-11-22
    功能描述：实现短语的权重提取,并写入目标文件中


"""

from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
from com.yangshengliang.config.config import Config
import time
import glob
import os
import csv


sep = os.sep
result_file_path = Config().config.get("result_path")
fenci_file_dir = sep.join([result_file_path, "fenci"])
tf_idf_file_path = sep.join([Config().config.get("result_path"), "tf-idf"])

try:
    if os.path.exists( tf_idf_file_path):
        pass
    else:
        os.makedirs( tf_idf_file_path)
        print("完成创建用于存放tf-idf结果的文件夹,完整路径为：%s" % ( tf_idf_file_path))
except Exception as e:
    print(e)

def getIdf(path):
    # 接受一个文件路径
    files = [os.path.abspath(file) for file in glob.glob(sep.join([path,'*.txt']))]
    for f in files:
        file_name = os.path.splitext(os.path.basename(f))[0]
        outfile = sep.join([tf_idf_file_path, time.strftime("%Y-%m-%d", time.localtime(time.time()))+"-%s-tfidf.csv"  % (file_name)])
        csvfile = open(outfile, 'w')
        writer = csv.writer(csvfile)
        writer.writerow(['词语', '权值'])
        csvfile.close()
        text = [line for line in open(f) if len(line) >1]
        vectorizer=CountVectorizer()
        transformer=TfidfTransformer()
        tfidf=transformer.fit_transform(vectorizer.fit_transform(text))
        word=vectorizer.get_feature_names()
        weight=tfidf.toarray()
        print("正在计算%s" % ( os.path.basename(f)))
        for i in range(len(weight)):#打印每类文本的tf-idf词语权重，第一个for遍历所有文本，第二个for便利某一类文本下的词语权重
            for j in range(len(word)):
                print(word[j], weight[i][j])
                with open(outfile, 'a') as csvfp:
                    writer = csv.writer(csvfp, delimiter=',')
                    writer.writerow([word[j],weight[i][j]])

        print("*******          %s 权值计算完成 进入下一个                  ******" % (os.path.basename(f)) )

    print("*******           权值计算完成 全部完成                  ******" )


if __name__ == "__main__":
    getIdf(fenci_file_dir)